package com.pdstudio.apapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.pdstudio.app.Application;
import com.pdstudio.bean.Warning;
import com.pdstudio.view.ViewHolderWarningList;
import com.pdstudio.weather.R;

public class WarningListAdapter extends BaseAdapter implements
		Application.EventHandler {

	private List<Warning> mWarnings;
	private LayoutInflater inflater;
	private Context mContext;
	private Application mApplication;

	public WarningListAdapter(Context context, List<Warning> warnings) {
		mWarnings = warnings;
		inflater = LayoutInflater.from(context);
		mContext = context;
		Application.mListeners.add(this);
		mApplication = Application.getInstance();
	}

	@Override
	public int getCount() {
		if (mWarnings == null) {
			return 0;
		} else {
			return (mWarnings.size());
		}
	}

	@Override
	public Object getItem(int position) {
		if (mWarnings == null) {
			return null;
		} else {
			return mWarnings.get(position);
		}
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolderWarningList holder;

		try {
			if (convertView == null) {
				convertView = inflater.inflate(R.layout.activity_warning, null);
				holder = new ViewHolderWarningList();
				/** 得到各个控件的对象 */
				holder.warningImg = (ImageView) convertView
						.findViewById(R.id.warning_img);
				holder.warningCity = (TextView) convertView
						.findViewById(R.id.warning_city);
				holder.warningLevel = (TextView) convertView
						.findViewById(R.id.warning_level);
				holder.warningTime = (TextView) convertView
						.findViewById(R.id.warning_time);
				holder.warningTip = (TextView) convertView
						.findViewById(R.id.warning_guide);
				convertView.setTag(holder);// 绑定ViewHolder对象
			}

			else {
				holder = (ViewHolderWarningList) convertView.getTag();// 取出ViewHolder对象
			}

			/** 设置TextView显示的内容，即我们存放在动态数组中的数据 */

			holder.warningCity.setText("预警城市："
					+ mWarnings.get(position).warningAddress);
			holder.warningLevel.setText("预警类型："
					+ mWarnings.get(position).warningType);
			holder.warningTime.setText("发布时间："
					+ mWarnings.get(position).postTime);
			holder.warningTip.setText("防御指引：" + "\n"
					+ mWarnings.get(position).tip);
			holder.warningImg.setBackgroundResource(mApplication
					.getWarningIcon(mWarnings.get(position).warningType));

		} catch (Exception e) {
			e.printStackTrace();
		}

		return convertView;
	}

	@Override
	public void onCityComplite() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onNetChange() {
		// TODO Auto-generated method stub

	}

}
